<?php
include_once('admin.inc.php');
$proModel = new Model('product');
$smarty = new Tpl();
$act = get_param('do');
switch($act){
	case "addPro":
		if( get_param('submit') ){
			$data = get_data();
			$data['description'] = htmlspecialchars($data['description']);
			if( substr($data['buy_link'],0,4) != 'http' ){
				$data['buy_link'] = "http://".$data['buy_link'];
			}
			$data['edittime'] = time();
			unset($data['id']);
			$reId = $proModel->value($data)->add();
			if( DEBUG ){
				echo 'addPro: ',$proModel->get_sql(),'<hr>';
			}
			
			if( $reId >0 ){
				// 添加成功，开始上传相关图片
				include_once(__LIB__.'Upload.class.php');
				$upCfg = array(
					// 文件大小：3M	
					'max_size' => 3*1024*1024,
					'safe_ext' => array('jpg','jpeg','png','gif','bmp','tiff'),
					'save_path' => __UPLOAD__.'product/'
					);
				// banner
				$up = new Upload('banner', $upCfg);
				$saveName = "banner_".$reId."_";
				$rsBanner = $up->save($saveName);
				if( $rsBanner ){
					$rsBanner = json_decode($rsBanner,true);
					$proPicModel = new Model('product_picture');
					foreach($rsBanner as $v){
						$data2 = array(
							'file' => $v['saveName'],
							'product_id' => $reId,
							'type' => 1,
							'addtime' => time()
							);
						$proPicModel->value($data2)->add();
						if( DEBUG ){
							echo 'addProPic: ',$proPicModel->get_sql(),'<hr>';
						}
					}
				}

				//细节图
				$up2 = new Upload('details', $upCfg);
				$saveName = "detail_".$reId."_";
				$rsDetail = $up2->save($saveName);
				if( $rsDetail ){
					$rsDetail = json_decode($rsDetail,true);
					$proPicModel = new Model('product_picture');
					foreach( $rsDetail as $v ){
						$data3 = array(
							'file' => $v['saveName'],
							'product_id' => $reId,
							'type' => 2,
							'addtime' => time()
							);
						$proPicModel->value($data3)->add();
						if( DEBUG ){
							echo 'addProPic: ',$proPicModel->get_sql(),'<hr>';
						}
					}
				}
				msg('添加成功','product.php?do=product');
			}else{
				@unlink(__UPLOAD__."product/$up->saveName");
				msg('添加失败，写入数据库失败');
			}
			exit;
		}
		$smarty->assign('do', 'addPro');
		$smarty->assign('pageTitle', '添加产品');
		$smarty->display('admin/addProduct.html');
	break;
	default:
	case "product":
		//产品列表
		$list = $proModel->order('`order`,`id` DESC')->get();
		if( DEBUG ){
			echo 'product: ',$proModel->get_sql(),'<hr>';
			dump($list);
		}
		//产品图片
		$proPicModel = new Model('product_picture');
		$picList = $proPicModel->order('`product_id`')->get();
		if( DEBUG ){
			echo 'proPic: ',$proPicModel->get_sql(),'<hr>';
			dump($picList);
		}
		// 将图片整合到产品数组中
		if( is_array($list) && is_array($picList) ){
			foreach( $list as $lk=>$lv ){
				foreach( $picList as $pk=>$pv ){
					if( $pv['product_id']==$lv['id'] ){
						$list[$lk]['piclist'][] = $pv;
					}
				}
			}
		}
		$smarty->assign("list",$list);

		$smarty->display("admin/product.html");
	break;
	case "delPro":
		$id = intval(get_param("id"));
		$rs = $proModel->where("id=$id")->del();
		//TODO:删除相关图片，静态页面
		$return = array('status'=>'fail','info'=>'删除失败');
		if( $rs ){
			ajax_return( array('status'=>'success', 'info'=>'删除成功') );
		}
		ajax_return($return);
	break;
	case "editPro":
		$id = intval(get_param('id'));
		if( empty($id) ){
			msg('非法操作',-1);
		}
		
		if( get_param('submit') ){
			$data = get_data();
			if( substr($data['buy_link'],0,4) != 'http' ){
				$data['buy_link'] = "http://".$data['buy_link'];
			}
			$data['description'] = htmlspecialchars($data['description']);
			$data['edittime'] = time();
			$rs = $proModel->where("id=$id")->value($data)->update();
			if( DEBUG ){
				echo 'editPro: ',$proModel->get_sql(),'<hr>';
			}
			
			if( $rs ){
				// 添加成功，开始上传相关图片
				include_once(__LIB__.'Upload.class.php');
				$upCfg = array(
					// 文件大小：3M	
					'max_size' => 3*1024*1024,
					'safe_ext' => array('jpg','jpeg','png','gif','bmp','tiff'),
					'save_path' => __UPLOAD__.'product/'
					);
				// banner
				$up = new Upload('banner', $upCfg);
				$saveName = "banner_".$id."_";
				$rsBanner = $up->save($saveName);
				if( $rsBanner ){
					$rsBanner = json_decode($rsBanner,true);
					$proPicModel = new Model('product_picture');
					foreach($rsBanner as $v){
						$data2 = array(
							'file' => $v['saveName'],
							'product_id' => $id,
							'type' => 1,
							'addtime' => time()
							);
						$proPicModel->value($data2)->add();
						if( DEBUG ){
							echo 'addProPic: ',$proPicModel->get_sql(),'<hr>';
						}
					}
				}
				unset($up);

				//细节图
				$up2 = new Upload('details', $upCfg);
				$saveName = "detail_".$id."_";
				$rsDetail = $up2->save($saveName);
				//dump($up2->get('error'));
				//dump($rsDetail);
				if( $rsDetail ){
					$rsDetail = json_decode($rsDetail,true);
					$proPicModel = new Model('product_picture');
					//dump($rsDetail);
					foreach( $rsDetail as $v ){
						$data3 = array(
							'file' => $v['saveName'],
							'product_id' => $id,
							'type' => 2,
							'addtime' => time()
							);
						$proPicModel->value($data3)->add();
						if( DEBUG ){
							echo 'uploadDetail: ',$proPicModel->get_sql(),'<hr>';
						}
					}
				}
				msg('修改成功');
			}else{
				@unlink(__UPLOAD__."product/$up->saveName");
				msg('修改失败，写入数据库失败');
			}
			exit;
		}
		//产品信息
		$proinfo = $proModel->where("id=$id")->get_one();
		$smarty->assign('proinfo', $proinfo);
		//产品图片
		$proPicModel = new Model('product_picture');
		$proPics = $proPicModel->where("product_id=$id")->get();
		$smarty->assign('proPics', $proPics);
		
		$smarty->assign('do', 'editPro');
		$smarty->display('admin/addProduct.html');
	break;
	case "delProPic":
		$id = intval(get_param('id'));
		if( empty($id) ){
			msg('非法操作');
		}
		$proPicModel = new Model('product_picture');
		$rs = $proPicModel->where("id=$id")->del();
		if( $rs ){
			ajax_return(array('status'=>'success', 'info'=>'删除成功'));
		}
		ajax_return(array('status'=>'fail', 'info'=>'删除失败'));
		
	break;
	case "ueImgUpload":
		$up = load_class('Upload');
		$up->config(array('save_path'=>__UPLOAD__.'product/'));
		$rs = $up->save('editor_'.rand(1000,9999));
		if( $rs ){
			$rs = json_decode($rs,true);
			$file = UPLOAD_URL.'product/'.$rs[0]['saveName'];
			$title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
			$state = "SUCCESS";
			echo "{'url':'".$file."','title':'".$title."','state':'".$state."'}";
		}else{
			$state = "FAILED ".$up->get('error');
			echo "{'url':'".$file."','title':'".$title."','state':'".$state."'}";
		}
	break;
	case "buildHtml":
		//导航
		$p = $proModel->order('`order`,id DESC')->get();
		$smarty->assign("product", $p);
		
		//产品图片
		$proPicModel = new Model('product_picture');
		
		//评论
		$comModel = new Model("comment");
		if( is_array($p) && count($p)>0 ){
			foreach($p as $v){
				//产品信息
				$smarty->assign("proinfo", $v);

				$proPicModel->free_result();
				$proPics = $proPicModel->where("product_id=".$v['id'])->get();
				$smarty->assign('proPics', $proPics);

				$comModel->free_result();
				$comms = $comModel->where("p_id=".$v['id'])->order('id DESC')->get();
				$smarty->assign("comms", $comms);
				
				$html = $smarty->fetch("product.html");
				if( !empty($html) ){
					$rs = file_put_contents(__ROOT__."product/".$v['id'].".html", $html);
				}
			}
		}
		if( $rs ){
			msg("生成成功");
		}else{
			msg("生成失败");
		}
	break;
}
